package com.langll.ai.redis;

import com.langll.ai.AIChatApplication;
import com.langll.ai.chat.RedisChatMemory;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.ai.chat.messages.Message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.Set;

/**
 * 删除所有redis缓存
 */
@Slf4j
@SpringBootTest(classes = AIChatApplication.class)
@RunWith(SpringRunner.class)
public class TestDeleteAll {

    @Autowired
    private RedisChatMemory redisChatMemory;
    @Autowired
    private  RedisTemplate<String, Message> redisTemplate;

    @Test
    public void testClean() {
        // 获取所有以 chatmemory: 开头的键
        Set<String> keys = redisTemplate.keys("chatmemory:*");
        if (keys != null && !keys.isEmpty()) {
            // 删除这些键
            redisTemplate.delete(keys);
        }

        Set<String> session_keys = redisTemplate.keys("chat_session:*");
        if (session_keys != null && !session_keys.isEmpty()) {
            // 删除这些键
            redisTemplate.delete(session_keys);
        }
    }
}
